package com.fxm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fxm.constant.RedisConstants;
import com.fxm.dao.SignMapper;
import com.fxm.domain.Sign;
import com.fxm.domain.User;
import com.fxm.returns.RespCode;
import com.fxm.returns.RespEntity;
import com.fxm.returns.RespTable;
import com.fxm.service.SignService;
import com.fxm.service.UserService;
import com.fxm.utils.RedisUtil;
import com.fxm.vo.param.RewardParam;
import com.fxm.vo.result.RewardResult;
import com.fxm.vo.result.SignResult;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 签到总览 服务实现类
 * </p>
 *
 * @author fxm
 * @since 2021-02-23
 */
@Slf4j
@Service
public class SignServiceImpl extends ServiceImpl<SignMapper, Sign> implements SignService {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public RespTable<List<SignResult>> signList(Integer page, Integer limit, String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getDeleted, 0);
        if (StringUtils.isNotBlank(username)) {
            queryWrapper.lambda().like(User::getUsername, username);
        }
        IPage<User> iPage = new Page<>(page, limit);
        IPage<User> pg = userService.page(iPage, queryWrapper);
        List<User> userList = pg.getRecords();
        if (Objects.isNull(userList) || userList.size() == 0) {
            return new RespTable(RespCode.OK, null, 0);
        }
        //数据组装
        List<SignResult> signResultList = dataPackage(userList);
        return new RespTable(RespCode.OK, signResultList, pg.getTotal());
    }

    /**
     * 数据组装
     * @param userList
     * @return
     */
    private List<SignResult> dataPackage(List<User> userList) {
        List<SignResult> signResultList = Lists.newArrayList();
        for (User user : userList) {
            SignResult signResult = SignResult.builder()
                    .id(user.getId())
                    .openid(user.getOpenid())
                    .username(user.getUsername())
                    .build();

            String json = redisUtil.get(RedisConstants.key.getSignIn(user.getOpenid()));
            signResult.setSign(Integer.parseInt(json));

            signResultList.add(signResult);
        }

        return signResultList;
    }

    @Override
    public RespEntity<RewardResult> rewardList() {
        QueryWrapper<Sign> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Sign::getDeleted, 0);
        List<Sign> signList = baseMapper.selectList(queryWrapper);
        //签到奖励数据组装
        RewardResult rewardResult = rewardDataPackage(signList);
        return new RespEntity(RespCode.SUCCESS, rewardResult);
    }

    /**
     * 签到奖励数据组装
     * @param signList
     * @return
     */
    private RewardResult rewardDataPackage(List<Sign> signList) {
        RewardResult rewardResult = new RewardResult();
        if (Objects.isNull(signList) || signList.size() == 0) {
            return rewardResult;
        }

        for (Sign sign : signList) {
            switch (sign.getDays()) {
                case 1 : rewardResult.setDays1(sign.getReward()); break;
                case 2 : rewardResult.setDays2(sign.getReward()); break;
                case 3 : rewardResult.setDays3(sign.getReward()); break;
                case 4 : rewardResult.setDays4(sign.getReward()); break;
                case 5 : rewardResult.setDays5(sign.getReward()); break;
                case 6 : rewardResult.setDays6(sign.getReward()); break;
                case 7 : rewardResult.setDays7(sign.getReward()); break;
            }
        }

        return rewardResult;
    }

    @Override
    public RespEntity updateReward(RewardParam rewardParam) {
        Sign sign = baseMapper.selectById(rewardParam.getId());
        sign.setReward(rewardParam.getReward());
        baseMapper.updateById(sign);
        return new RespEntity(RespCode.SUCCESS);
    }
}
